package LCR;

import java.util.Arrays;

public class threeSum {
    public static void main(String[] args) {
        int[][] ret = new int[5][3];

    }
    public int[][] threeSum(int[] nums) {
        Arrays.sort(nums);
        int[][] ret = new int[nums.length*6][3];
        int index = 0;
        for (int i = 0; i < nums.length;) {
            int tar = nums[i];
            int left = i + 1;
            int right = nums.length - 1;
            while (left < right) {
                if (nums[left] + nums[right] + tar == 0) {
                    ret[index][0] = tar;

                    ret[index][1] = nums[left];
                    ret[index][2] = nums[right];
                    index++;
                    left++;
                    right--;
                    // 去重
                    while (left < right && nums[left] == nums[left - 1]) {
                        left++;
                    }
                    while (left < right && nums[right] == nums[right + 1]) {
                        right--;
                    }
                } else if (nums[left] + nums[right] + tar > 0) {
                    right--;
                } else {
                    left++;
                }
            }
            //去重i
            i++;
            while (i < nums.length && nums[i] == nums[i - 1]) {
                i++;

            }
        }
        int[][] ans = new int[index][3];
        for (int i = 0; i < index; i++) {
            ans[i] = ret[i];
        }
        return ans;

    }
}
